"use strict";

const imeta = require("./imeta-code-generator");

function handleExportMetadata(){
    app.elementPickerDialog.showDialog("Select a model Package", null, type.UMLPackage).then(function ({buttonId, returnValue}) {
        if (buttonId === 'ok') {
            var context={
                projectName: app.project.getProject().name,
                debug: app.preferences.get("imetaframework.debug.status"),
                indents: app.preferences.get("imetaframework.gen.indents"),
                idldoc: app.preferences.get("imetaframework.gen.idldoc")
            }
            var md = new imeta.metadata(returnValue, context);
            // console.log("You selected: ", returnValue)
            /*var filters = [
                { name: "Text Files", extensions: [ "xml" ] }
            ]*/
            var filename = app.dialogs.showSaveDialog("Export model as...", "imeta_" + context.projectName + ".xml")
            if (filename) {
                context['path'] = filename;
                md.export(imeta.printers.file);
                app.toast.info("导出iMeta成功，文件路径：" + filename);
            } else {
                md.export(imeta.printers.console);
                app.toast.info("导出iMeta成功");
            }
        }
    })
}

function handleExportMetadataDebug(){
    app.elementPickerDialog.showDialog("Select a model Package", null, type.UMLPackage).then(function ({buttonId, returnValue}) {
        if (buttonId === 'ok') {
            var context={
                projectName: app.project.getProject().name,
                debug: app.preferences.get("imetaframework.debug.status"),
                indents: app.preferences.get("imetaframework.gen.indents"),
                idldoc: app.preferences.get("imetaframework.gen.idldoc")
            }
            var md = new imeta.metadata(returnValue, context);
            md.export(imeta.printers.console);
            app.toast.info("导出iMeta成功");
        }
    })
}

function handleConfigure() {
    app.commands.execute("application:preferences", "imetaframework");
}

function handleDetectDataType() {
    var primitives = app.repository.select('@UMLPackage[name='+app.preferences.get("imetaframework.detect.datatype")+']');
    if (primitives.length == 0) {
        app.dialogs.showErrorDialog("Please Import imeta-fragment.mfj first.")
    } else {
        var context = {
            primitivePackage: primitives[0],
            attributes: app.repository.select('@UMLAttribute')
        };
        var detector = new imeta.detect(context);
        var msg = detector.detectDataType();
        app.toast.info(msg);
        console.info("[detect]", msg);
    }
}

function init () {
    app.commands.register('imetaframework:export-model', handleExportMetadata);
    app.commands.register('imetaframework:export-model-debug', handleExportMetadataDebug);
    app.commands.register('imetaframework:open-config', handleConfigure);
    app.commands.register('imetaframework:detect-datatype', handleDetectDataType);
}

exports.init = init